package com.stylefeng.guns.modular.system.warpper;

import java.text.DecimalFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.lang.StringUtils;

import com.stylefeng.guns.core.base.warpper.BaseControllerWarpper;
import com.stylefeng.guns.core.common.constant.factory.ConstantFactory;
import com.stylefeng.guns.core.util.DateUtil;
import com.stylefeng.guns.modular.system.model.IndustryRank;
import com.stylefeng.guns.modular.system.model.Realtime;

public class MyRealTimeMonitorWarpper extends BaseControllerWarpper{
	
	private int type;
	
	private Map<String,Boolean> keys;

	public MyRealTimeMonitorWarpper(List<Map<String, Object>> list,int type,Map<String,Boolean> keys) {
        super(list);
        this.type = type;
        this.keys = keys;
    }

    @Override
    public void warpTheMap(Map<String, Object> map) {
    	DecimalFormat df = new DecimalFormat("0.00");
    	String gScode = (String)map.get("gScode");
    	List<Map<String,Object>> maps = ConstantFactory.me().selectCchicStatus(gScode, DateUtil.formatDate((Date)map.get("gTdate"), "yyyy-MM-dd"));
    	if(type == 1){
    		List<Realtime> realtimes = ConstantFactory.me().selectRealTimeDate(gScode,DateUtil.formatDate((Date)map.get("gTdate"), "yyyy-MM-dd"));
    		for(Realtime read :realtimes){
        		putType(read);
        	}
        	map.put("realTimes", realtimes);
        	StringBuffer buffer = new StringBuffer();
        	buffer.append("<b><div style='float: left; width: 80px;'>持仓时间</div><div style='float: left; width: 70px;'>买入(笔)</div><div style='float: left; width: 70px;'>卖出(笔)</div><div style='float: left; width: 70px;'>净买入(笔)</div><div style='float: left; width: 70px;'>涨跌幅(%)</div>").append("</br>");
    		double buy = 0;
    		double seller = 0;
    		for(Map<String,Object> v : maps){
        		if(DateUtil.formatDate((Date)map.get("gTdate"), "yyyy-MM-dd").equals((String)v.get("gtime"))){
        		    buy = (Double)v.get("buytotal");
            	    seller = (Double)v.get("sellertotal");
        		}
        		buffer.append("<div style='float: left; width: 70px;'>" + (String)v.get("gtime") + "</div>")
        		.append("<div style='float: left; width: 70px;'><font color='red'>" + df.format((Double)v.get("buytotal")) + "</div></font>")
        		.append("<div style='float: left; width: 70px;'><font color='green'>" + df.format((Double)v.get("sellertotal"))  + "</div></font>");
        		double jbuy = (Double)v.get("buytotal") - (Double)v.get("sellertotal");
        		if(jbuy >0){
        			buffer.append("<div style='float: left; width: 70px;'><font color='red'>" + df.format(jbuy)  + "</div></font>");
        		}else{
        			buffer.append("<div style='float: left; width: 70px;'><font color='green'>" + df.format(jbuy)  + "</div></font>");
        		}
        		double gZdf = (Double)v.get("gZdf");
        		if(gZdf > 0){
        			buffer.append("<div style='float: left; width: 70px;'><font color='red'><b>" + v.get("gZdf") + "</b></font></div>").append("</br>");
        		}else{
        			buffer.append("<div style='float: left; width: 70px;'><font color='green'><b>" + v.get("gZdf") + "</b></font></div>").append("</br>");
        		}
        	}
    		map.put("buy", df.format(buy));
    		map.put("seller", df.format(seller));
    		map.put("jbuy", (buy - seller) > 0 ? "<font size='2' color='red'><b>"+df.format((buy - seller))+"</b></font>" : "<font size='2' color='green'><b>"+df.format((buy - seller))+"</b></font>");
        	buffer.append("</b>");
        	map.put("chicang", buffer.toString());
    	}else{
    		List<Realtime> realtimes = ConstantFactory.me().selectRealTimeDate(gScode,DateUtil.formatDate(new Date(), "yyyy-MM-dd"));
    		for(Realtime read :realtimes){
        		putType(read);
        	}
        	map.put("realTimes", realtimes);
        	StringBuffer buffer = new StringBuffer();
        	buffer.append("<b><div style='float: left; width: 80px;'>持仓时间</div><div style='float: left; width: 70px;'>买入(笔)</div><div style='float: left; width: 70px;'>卖出(笔)</div><div style='float: left; width: 70px;'>净买入(笔)</div><div style='float: left; width: 70px;'>涨跌幅(%)</div>").append("</br>");
    		double buy = 0;
    		double seller = 0;
    		for(Map<String,Object> v : maps){
        		if(DateUtil.formatDate((Date)map.get("gTdate"), "yyyy-MM-dd").equals((String)v.get("gtime"))){
        		    buy = (Double)v.get("buytotal");
            	    seller = (Double)v.get("sellertotal");
        		}
        		buffer.append("<div style='float: left; width: 70px;'>" + (String)v.get("gtime") + "</div>")
        		.append("<div style='float: left; width: 70px;'><font color='red'>" + df.format((Double)v.get("buytotal")) + "</div></font>")
        		.append("<div style='float: left; width: 70px;'><font color='green'>" + df.format((Double)v.get("sellertotal"))  + "</div></font>");
        		double jbuy = (Double)v.get("buytotal") - (Double)v.get("sellertotal");
        		if(jbuy >0){
        			buffer.append("<div style='float: left; width: 70px;'><font color='red'>" + df.format(jbuy)  + "</div></font>");
        		}else{
        			buffer.append("<div style='float: left; width: 70px;'><font color='green'>" + df.format(jbuy)  + "</div></font>");
        		}
        		double gZdf = (Double)v.get("gZdf");
        		if(gZdf > 0){
        			buffer.append("<div style='float: left; width: 70px;'><font color='red'><b>" + v.get("gZdf") + "</b></font></div>").append("</br>");
        		}else{
        			buffer.append("<div style='float: left; width: 70px;'><font color='green'><b>" + v.get("gZdf") + "</b></font></div>").append("</br>");
        		}
        		
        	}
    		map.put("buy", df.format(buy));
    		map.put("seller", df.format(seller));
    		map.put("jbuy", (buy - seller) > 0 ? "<font size='2' color='red'><b>"+df.format((buy - seller))+"</b></font>" : "<font size='2' color='green'><b>"+df.format((buy - seller))+"</b></font>");
        	buffer.append("</b>");
        	map.put("chicang", buffer.toString());
    	}
    	IndustryRank industryRank = ConstantFactory.me().getIndustryRank((String)map.get("gOwerBk"),null);
    	if(industryRank != null){
    		map.put("zdf", industryRank.getgZdf() > 0 ? "<font size='2' color='red'><b>"+industryRank.getgZdf()+"</b></font>" : "<font size='2' color='green'><b>"+industryRank.getgZdf()+"</b></font>");
    	}else{
    		map.put("zdf", "<font size='2' color='red'><b>0.00</b></font>");
    	}
    	map.put("gOwerBk","<font color='red'><b>"+map.get("gOwerBk")+"</font></b>");
    	String gJgcydType = ((String)map.get("gJgcydType"));
		String font = "";
		if(gJgcydType.equals("不控盘")){
			font = "<font color='DimGray'><b>不控盘</b></font>";
		}else if(gJgcydType.equals("轻度控盘")){
			font = "<font color='BurlyWood'><b>轻度控盘</b></font>";
		}else if(gJgcydType.equals("中度控盘")){
			font = "<font color='Chocolate'><b>中度控盘</b></font>";
		}else{
			font = "<font color='Brown'><b>完全控盘</b></font>";
		}
		map.put("gJgcydType", font);
		
		String gReason = (String)map.get("gReason");
		if(StringUtils.isEmpty(gReason)){
			map.put("gReason", "");
		}else{
			map.put("gReason", "<b>"+gReason+"</b>");
		}
		map.put("gZsz", df.format(Math.abs(((Double)map.get("gZsz") / 100000000))));
		
		StringBuffer buffer = new StringBuffer();
		String[] concepts = StringUtils.split((String)map.get("gConceptBk"), "、");
		for(String c : concepts){
			Map<Boolean,Boolean> isMaps = isExits(c);
			for(Entry<Boolean, Boolean> e : isMaps.entrySet()){
				if(e.getKey()){
					if(e.getValue()){
						buffer.append("<font color='green'><b class='blink_green'>"+c+"</font></b>,");
					}else{
						buffer.append("<font color='Brown'><b class='blink_red'>"+c+"</font></b>,");
					}
				}else{
					buffer.append("<font color='Chocolate'><b>"+c+"</font></b>,");
				}
			}
		}
		map.put("gConceptBk", buffer.toString());
		
    }
    
    private Map<Boolean,Boolean> isExits(String key){
    	Map<Boolean,Boolean> isMaps = new HashMap<Boolean, Boolean>();
    	boolean isExits = false;
    	for(Entry<String, Boolean> e : keys.entrySet()){
    		if(StringUtils.contains(key, e.getKey())){
				if(e.getValue()){
					isMaps.put(true, true);
					isExits = true;
				}else{
					isMaps.put(true, false);
					isExits = true;
				}
    		}
    	}
    	if(!isExits){
    		isMaps.put(false, false);
    	}
    	return isMaps;
    }
    
    private void putType(Realtime read){
    	if(read.getgType() == 8214l){
    		read.setTemp("<font color='green'><b>60日新低</font></b>");
    		read.setgDec("<font color='green'><b>"+read.getgDec()+"</font></b>");
    	}else if(read.getgType() == 8202l){
    		read.setTemp("<font color='red'><b>快速反弹</font></b>");
    		read.setgDec("<font color='red'><b>"+read.getgDec()+"</font></b>");
    	}else if(read.getgType() == 8201l){
    		read.setTemp("<font color='red'><b>快速反弹</font></b>");
    		read.setgDec("<font color='red'><b>"+read.getgDec()+"</font></b>");
    	}else if(read.getgType() == 8194l){
    		read.setTemp("<font color='green'><b>大笔卖出</font></b>");
    		read.setgDec("<font color='green'><b>"+read.getgDec()+"</font></b>");
    	}else if(read.getgType() == 128l){
    		read.setTemp("<font color='green'><b class='blink_green'>有大卖盘</font></b>");
    		read.setgDec("<font color='green'><b>"+read.getgDec()+"</font></b>");
    	}else if(read.getgType() == 8203l){
    		read.setTemp("<font color='green'><b>高台跳水</font></b>");
    		read.setgDec("<font color='green'><b>"+read.getgDec()+"</font></b>");
    	}else if(read.getgType() == 8193l){
    		read.setTemp("<font color='red'><b>大笔买入</font></b>");
    		read.setgDec("<font color='red'><b>"+read.getgDec()+"</font></b>");
    	}else if(read.getgType() == 8l){
    		read.setTemp("<font color='green'><b>封跌停板</font></b>");
    		read.setgDec("<font color='green'><b>"+read.getgDec()+"</font></b>");
    	}else if(read.getgType() == 32l){
    		read.setTemp("<font color='red'><b>打开跌停板</font></b>");
    		read.setgDec("<font color='red'><b>"+read.getgDec()+"</font></b>");
    	}else if(read.getgType() == 64l){
    		read.setTemp("<font color='red'><b class='blink_red'>有大买盘</font></b>");
    		read.setgDec("<font color='red'><b>"+read.getgDec()+"</font></b>");
    	}else if(read.getgType() == 4l){
    		read.setTemp("<font color='red'><b>封涨停板</font></b>");
    		read.setgDec("<font color='red'><b>"+read.getgDec()+"</font></b>");
    	}else if(read.getgType() == 8215l){
    		read.setTemp("<font color='red'><b>60日大幅上涨</font></b>");
    		read.setgDec("<font color='red'><b>"+read.getgDec()+"</font></b>");
    	}else if(read.getgType() == 16l){
    		read.setTemp("<font color='green'><b>打开涨停板</font></b>");
    		read.setgDec("<font color='green'><b>"+read.getgDec()+"</font></b>");
    	}else if(read.getgType() == 8213l){
    		read.setTemp("<font color='red'><b>60日新高</font></b>");
    		read.setgDec("<font color='red'><b>"+read.getgDec()+"</font></b>");
    	}else if(read.getgType() == 8204l){
    		read.setTemp("<font color='green'><b>加速下跌</font></b>");
    		read.setgDec("<font color='green'><b>"+read.getgDec()+"</font></b>");
    	}else if(read.getgType() == 8216l){
    		read.setTemp("<font color='green'><b>60日大幅下跌</font></b>");
    		read.setgDec("<font color='green'><b>"+read.getgDec()+"</font></b>");
    	}else if(read.getgType() == 8212l){
    		read.setTemp("<font color='green'><b>向下缺口</font></b>");
    		read.setgDec("<font color='green'><b>"+read.getgDec()+"</font></b>");
    	}else if(read.getgType() == 8211l){
    		read.setTemp("<font color='red'><b>向上缺口</font></b>");
    		read.setgDec("<font color='red'><b>"+read.getgDec()+"</font></b>");
    	}else if(read.getgType() == 8207l){
    		read.setTemp("<font color='red'><b>竞价上涨</font></b>");
    		read.setgDec("<font color='red'><b>"+read.getgDec()+"</font></b>");
    	}else if(read.getgType() == 8208l){
    		read.setTemp("<font color='green'><b>竞价下跌</font></b>");
    		read.setgDec("<font color='green'><b>"+read.getgDec()+"</font></b>");
    	}else if(read.getgType() == 8209l){
    		read.setTemp("<font color='red'><b>高开5日线</font></b>");
    		read.setgDec("<font color='red'><b>"+read.getgDec()+"</font></b>");
    	}else if(read.getgType() == 8210l){
    		read.setTemp("<font color='green'><b>低开5日线</font></b>");
    		read.setgDec("<font color='green'><b>"+read.getgDec()+"</font></b>");
    	}
    }
}
